﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Rio.Applications.Web.Services;
using Rio.Framework.Pages;
using Rio.Framework.Pages.AppCode.Enums;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Controls;
using Rio.Framework.Pages.Enums;
using Rio.Framework.Pages.Events;
using Rio.Framework.Pages.Manager;
using Rio.Framework.Pages.Viewer;
using Rio.Framework.WCFService;
using Rio.Framework.WCFService.RioWcfServices;

namespace Rio.Application.RioWeb.Import.Views
{
    public partial class ImportView
    {
        #region Attributes

        protected DispatcherTimer m_dtRioscanTimer;
        protected string m_sRefreshScanGuid = String.Empty;
        protected WCFServiceClient m_oWCFServiceClient;

        private FolderPage.SelectModeDocument DisplayMode;
        private IndexPanel m_oControlImportPanel;

        private ThumbnailPanel _thumbnailViewer;
        private UCViewer _singleViewer;
        private ListViewer _listViewer;

        private bool selectAllImageAfterScan = false;
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImportPage"/> class.
        /// </summary>
        public ImportView()
        {
            InitializeComponent();

            if (HtmlPage.Document.DocumentUri.OriginalString.Contains("?"))
                QueryByUriManager.Init(HtmlPage.Document.DocumentUri, QueryByUriType.Import);

            m_oWCFServiceClient = ServiceManager.GetRioWCFServiceClient();
            m_oWCFServiceClient.RefreshFileExistsCompleted += L_oWCFServiceClient_RefreshFileExistsCompleted;
            m_oWCFServiceClient.CreateRefreshFileCompleted += L_oWCFServiceClient_CreateRefreshFileCompleted;

            if (LoginManager.Instance.CurrentUser != null)
                Initialization();
            else
            {
                //if (LoginManager.Instance.AuthenticationMethod == LoginDomainServiceEnums.AuthenticationMethod.SSO)
                //{
                    LoginManager.Instance.AuthenticatedUser += (sender, args) => Initialization();
                //}
            }
        }

        #endregion

        public void Initialization()
        {
            InitListThumbnailsAndViewerControl();

            headMenu.LoadUser(ApplicationName.RioImport);

            m_oControlImportPanel = new IndexPanel(_thumbnailViewer, _singleViewer, _listViewer);
            m_oControlImportPanel.GoToThumbnailMode += m_oControlImportPanel_GoToThumbnailMode;
            m_oControlImportPanel.BeforeIndexing += m_oControlImportPanel_BeforeIndexing;
            ControlImportPanelParent.Child = m_oControlImportPanel;
            m_oControlImportPanel.UseChronoGeneration = true;

            //**_listViewer events
            _listViewer.CurrentDisplayMode = AppDisplayMode.Import;
            _listViewer.ActivateThumbnailMode += ActivateThumbnailMode;
            _listViewer.ActivateViewerMode += ActivateViewerMode;
            _listViewer.ItemsSelectedToDisplay += ItemsSelectedToDisplay;
            _listViewer.ItemsSelectedFileName += ItemsSelectedFileName;
            _listViewer.DirectorySelectedToDisplay += ListViewer_DirectorySelectedToDisplay;
            _listViewer.DirectorySelected += ThumbnailSingleViewerDirectorySelected;
            _listViewer.ImportRequest += ImportRequest;
            _listViewer.ScanRequest += ScanRequest;

            //**_viewerControl events
            _singleViewer.CurrentDisplayMode = AppDisplayMode.Import;
            _singleViewer.ActivateListMode += ActivateListMode;
            _singleViewer.ActivateThumbnailMode += ActivateThumbnailMode;
            _singleViewer.ItemsSelectedFileName += SingleViewerItemsSelectedFileName;
            _singleViewer.DirectorySelectedToDisplay += SingleViewerDirectorySelectedToDisplay;
            _singleViewer.DirectorySelected += ThumbnailSingleViewerDirectorySelected;
            _singleViewer.ImportRequest += ImportRequest;
            _singleViewer.ScanRequest += ScanRequest;
            _singleViewer.ImportedDocumentDeleted += SingleViewerImportedDocumentDeleted;

            //**_viewerControl events
            _thumbnailViewer.CurrentDisplayMode = AppDisplayMode.Import;
            _thumbnailViewer.FileInPostOrder = true;
            _thumbnailViewer.EndOfImport += ViewerControl_EndOfImport;
            _thumbnailViewer.ActivateViewerMode += ActivateViewerMode;
            _thumbnailViewer.ActivateListMode += ActivateListMode;
            _thumbnailViewer.ItemsSelectedToDisplay += ItemsSelectedToDisplay;
            _thumbnailViewer.ItemsSelectedFileName += ItemsSelectedFileName;
            _thumbnailViewer.RefreshThumbnails += ThumbnailViewerRefreshThumbnails;
            _thumbnailViewer.DirectorySelectedToDisplay += ThumbnailViewerDirectorySelectedToDisplay;
            _thumbnailViewer.DirectorySelected += ThumbnailSingleViewerDirectorySelected;
            _thumbnailViewer.MaxNumberOfFilesImported = Int32.Parse(GlobalWebContext.RioAppSettings["MaxNumberOfFilesImported"]);
            _thumbnailViewer.ImportedDocumentRefreshed += ThumbnailViewerImportedDocumentRefreshed;
            _thumbnailViewer.ImportRequest += ImportRequest;
            _thumbnailViewer.ScanRequest += ScanRequest;
            _thumbnailViewer.ImportDocumentProgressed += ThumbnailViewerImportDocumentProgressed;

            if (GlobalWebContext.CurrentUser.CTX_DisplayMode)
            {
                _singleViewer.Visibility = Visibility.Collapsed;
                _thumbnailViewer.Visibility = Visibility.Visible;
                DisplayMode = FolderPage.SelectModeDocument.Thumbnail;
            }
            else
            {
                _singleViewer.Visibility = Visibility.Visible;
                _thumbnailViewer.Visibility = Visibility.Collapsed;
                DisplayMode = FolderPage.SelectModeDocument.Viewer;
            }

            busyIndicator.Visibility = Visibility.Collapsed;

            //An import by uri is performed
            if (QueryByUriManager.ImportInstance != null)
            {
                m_oControlImportPanel.PerformImportByUri();
            }
        }

        #region Events implementation

        /// <summary>
        /// Event raised when selecting an item is selected in the thumbnail panel
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DocumentSelectedEventArgs"/> instance containing the event data.</param>
        private void ItemsSelectedFileName(object sender, DocumentSelectedEventArgs args)
        {
            if (args.ListAllFileName != null && args.ListAllFileName.Count > 0)
            {
                m_oControlImportPanel.m_oListSelectedFilename = args.ListAllSelectedFiles;
            }
            else
            {
                m_oControlImportPanel.m_oListSelectedFilename.Clear();
            }
        }

        /// <summary>
        /// Event raised when clicking on a thumbnail image
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DocumentSelectedEventArgs"/> instance containing the event data.</param>
        private void ItemsSelectedToDisplay(object sender, DocumentSelectedEventArgs args)
        {
            _singleViewer.IndexPage = 1;
            _singleViewer.m_oItemsSource = args.ListAllRioImages;
            _singleViewer.CurrentSelectedDirectory = ((UCViewerCore)sender).CurrentSelectedDirectory;
            _singleViewer.SelectedDocumentIndex = args.SelectedDocumentIndex;

            //Activate viewer mode
            _singleViewer.Visibility = Visibility.Visible;
            _thumbnailViewer.Visibility = Visibility.Collapsed;
            _listViewer.Visibility = Visibility.Collapsed;
            DisplayMode = FolderPage.SelectModeDocument.Viewer;

            //Load document into the viewer
            _singleViewer.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, _thumbnailViewer.ListAllDocFileName, args.SelectedDocumentIndex, false);

        }

        /// <summary>
        /// Event switching from a mode to Viewer mode
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ActivateViewerMode(object sender, EventArgs e)
        {
            _listViewer.Visibility = Visibility.Collapsed;
            _singleViewer.Visibility = Visibility.Visible;
            _thumbnailViewer.Visibility = Visibility.Collapsed;

            _singleViewer.UserId = _thumbnailViewer.UserId;
            _singleViewer.ListAllDocFileName = _thumbnailViewer.ListAllDocFileName;
            _singleViewer.m_oItemsSource = _thumbnailViewer.m_oItemsSource;
            _singleViewer.CurrentSelectedDirectory = _thumbnailViewer.CurrentSelectedDirectory;

            switch (DisplayMode)
            {
                case FolderPage.SelectModeDocument.Thumbnail:
                    if (((ListBox)_thumbnailViewer.FindName("ListBoxVignette")).SelectedIndex != -1)
                    {
                        _singleViewer.IndexPage = 1;
                        _singleViewer.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, _thumbnailViewer.ListAllDocFileName, ((ListBox)_thumbnailViewer.FindName("ListBoxVignette")).SelectedIndex, false);
                    }
                    else
                    {
                        _singleViewer.IndexPage = 1;
                        _singleViewer.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, _thumbnailViewer.ListAllDocFileName, 0, false);
                    }
                    break;

                case FolderPage.SelectModeDocument.List:
                    _singleViewer.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId,
                                                        _listViewer.ListAllDocFileName,
                                                        _listViewer.SelectedImageIndex > -1
                                                            ? _listViewer.SelectedImageIndex
                                                            : 0, false);
                    break;
            }

            DisplayMode = FolderPage.SelectModeDocument.Viewer;
            _singleViewer.ManageToolbarState();
        }

        /// <summary>
        /// Event switching from a mode to Thumbnail mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ActivateThumbnailMode(object sender, EventArgs e)
        {
            _listViewer.Visibility = Visibility.Collapsed;

            _singleViewer.Visibility = Visibility.Collapsed;
            _singleViewer.ClearViewer();

            _thumbnailViewer.Visibility = Visibility.Visible;


            switch (DisplayMode)
            {
                case FolderPage.SelectModeDocument.List:
                    if (_listViewer.SelectedImages != null)
                    {
                        _thumbnailViewer.SelectedImages = _listViewer.SelectedImages;
                    }
                    _listViewer.SetActuallyRankingList();
                    _thumbnailViewer.RankedListFromViewer = _listViewer.RankedListFromViewer;

                    //_thumbnailViewer.ListItemSource = new ObservableCollection<ImageRio>(_listViewer.m_oItemsSource);

                    break;
                case FolderPage.SelectModeDocument.Viewer:
                    _thumbnailViewer.SelectedImages = _singleViewer.GetSelectedImage<ImageRio>();
                    _thumbnailViewer.RankedListFromViewer = _singleViewer.ListAllDocFileName;
                    _thumbnailViewer.LoadVignettes(_singleViewer.SelectedDocumentIndex);

                    break;
            }

            m_oControlImportPanel.m_oListSelectedFilename = _thumbnailViewer.GetSelectedImage<ImageRio>();
            DisplayMode = FolderPage.SelectModeDocument.Thumbnail;
            _thumbnailViewer.ManageToolbarState();

        }

        /// <summary>
        /// Event switching from a mode to List mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ActivateListMode(object sender, EventArgs e)
        {
            _singleViewer.Visibility = Visibility.Collapsed;
            _singleViewer.ClearViewer();

            _thumbnailViewer.Visibility = Visibility.Collapsed;

            _listViewer.ListAllDocFileName = _thumbnailViewer.ListAllDocFileName;
            _listViewer.m_oItemsSource = _thumbnailViewer.m_oItemsSource;
            _listViewer.UserId = _thumbnailViewer.UserId;
            //_listViewer.Images = new ObservableCollection<ImageRio>(_thumbnailViewer.m_oItemsSource);
            _listViewer.Images = _thumbnailViewer.m_oItemsSource;
            _listViewer.CurrentSelectedDirectory = ((UCViewerCore)sender).CurrentSelectedDirectory;
            _listViewer.Visibility = Visibility.Visible;

            switch (DisplayMode)
            {
                case FolderPage.SelectModeDocument.Thumbnail:
                    if (_thumbnailViewer.SelectedImages != null && _thumbnailViewer.SelectedImages.Count > 0)
                    {
                        _listViewer.SelectedImages = _thumbnailViewer.SelectedImages;
                    }
                    _thumbnailViewer.SetActuallyRankingList();
                    _listViewer.RankedListFromViewer = _thumbnailViewer.RankedListFromViewer;
                    break;
                case FolderPage.SelectModeDocument.Viewer:
                    _listViewer.SelectedImages = _singleViewer.GetSelectedImage<ImageRio>();
                    break;
            }

            DisplayMode = FolderPage.SelectModeDocument.List;
            _listViewer.ManageToolbarState();
        }

        /// <summary>
        /// Handles the ImportRequest event of the _listViewer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ImportRequest(object sender, EventArgs e)
        {
            headMenu.SuspendNotification();

            IEnumerable<FileInfo> filesToImport = null;
            if (_thumbnailViewer.SelectFilesToImport(ref filesToImport))
            {
                if (_thumbnailViewer.Visibility == Visibility.Collapsed)
                {
                    ActiveVignetteModeForImportation(filesToImport);
                }
                else
                {
                    _thumbnailViewer.ImportFiles(filesToImport);
                }
            }
        }

        /// <summary>
        /// Handles the ScanRequest event of the _listViewer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ScanRequest(object sender, EventArgs e)
        {
            headMenu.SuspendNotification();

            if (_thumbnailViewer.Visibility.Equals(Visibility.Collapsed))
            {
                ActiveVignetteModeForImportation();
                _thumbnailViewer.RefreshThumbnails += ThumbnailViewerRefreshThumbnailsForScan;
            }
            else
            {
                SplashScreenManager.Wait(this);
            }
            _thumbnailViewer.SetActuallyRankingList();

            m_sRefreshScanGuid = GlobalWebContext.CurrentUser.RefreshScanGuid;
            m_oWCFServiceClient.CreateRefreshFileAsync(m_sRefreshScanGuid);

            Redirection.RioScan("A");
            m_dtRioscanTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 2000) };
            m_dtRioscanTimer.Tick += m_dtRioscanTimer_Tick;
        }

        #region ListViewer

        /// <summary>
        /// Event raised when double click a directory in the listviewer
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs"/> instance containing the event data.</param>
        private void ListViewer_DirectorySelectedToDisplay(object sender, DirectorySelectedEventArgs args)
        {
            _listViewer.SelectedDocumentIndex = 0;
            _listViewer.CurrentSelectedDirectory = args.DirectoryName;
            _listViewer.CurrentSelectedImage = args.SelectedDirectory;

            _thumbnailViewer.SelectedDocumentIndex = 0;
            _thumbnailViewer.CurrentSelectedDirectory = args.DirectoryName;
            _thumbnailViewer.CurrentSelectedImage = args.SelectedDirectory;
            _thumbnailViewer.LoadVignettes(0);

            //**disable button import
            _listViewer.ScanImportIsEnabled = args.DirectoryMode != DirectorySelectedEventArgs.Mode.OpenDirectory;

            _listViewer.RefreshItems();
            _listViewer.Visibility = Visibility.Visible;
            _singleViewer.Visibility = Visibility.Collapsed;
            _thumbnailViewer.Visibility = Visibility.Collapsed;
        }

        #endregion

        #region Thumbnail

        /// <summary>
        /// Event raised when selecting an item is selected in the thumbnail panel
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs"/> instance containing the event data.</param>
        private void ThumbnailSingleViewerDirectorySelected(object sender, DirectorySelectedEventArgs args)
        {
            if (args.SelectedDirectory != null && args.SelectedDirectory.FileList != null && args.SelectedDirectory.FileList.Count > 0)
            {
                m_oControlImportPanel.m_oListSelectedFilename = _thumbnailViewer.GetSelectedImage<ImageRio>();
            }
            else
            {
                m_oControlImportPanel.m_oListSelectedFilename.Clear();
            }
        }

        /// <summary>
        /// Lists the vignette_ imported document refreshed.
        /// </summary>
        /// <param name="p_oImageRioCollection">The p_o image rio collection.</param>
        private void ThumbnailViewerImportedDocumentRefreshed(System.Collections.ObjectModel.ObservableCollection<ImageRio> p_oImageRioCollection)
        {
            _thumbnailViewer.ThumbnailPanel_ImportedDocumentRefreshed(p_oImageRioCollection);
            _listViewer.Images = _thumbnailViewer.m_oItemsSource;

            //Use to reselect images after scan process
            if(selectAllImageAfterScan)
            {
                _thumbnailViewer.SelectImages();
                selectAllImageAfterScan = false;
            }
        }

        /// <summary>
        /// Event raised when double clicking on a thumbnail directory
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs"/> instance containing the event data.</param>
        private void ThumbnailViewerDirectorySelectedToDisplay(object sender, DirectorySelectedEventArgs args)
        {
            _thumbnailViewer.SelectedDocumentIndex = 0;
            _thumbnailViewer.CurrentSelectedDirectory = args.DirectoryName;
            _thumbnailViewer.CurrentSelectedImage = args.SelectedDirectory;
            _thumbnailViewer.LoadVignettes(0);

            //**disable button import
            _thumbnailViewer.ScanImportIsEnabled = args.DirectoryMode != DirectorySelectedEventArgs.Mode.OpenDirectory;

            _listViewer.Visibility = Visibility.Collapsed;
            _singleViewer.Visibility = Visibility.Collapsed;
            _thumbnailViewer.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Event raised when clicking on the thumbnail refresh button
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ThumbnailViewerRefreshThumbnails(object sender, EventArgs e)
        {
            if (DisplayMode == FolderPage.SelectModeDocument.Viewer)
            {
                _singleViewer.m_oItemsSource = _thumbnailViewer.m_oItemsSource;
                _singleViewer.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId,
                                                    _thumbnailViewer.ListAllDocFileName, 0, false);
            }
            _thumbnailViewer.RefreshThumbnails -= ThumbnailViewerRefreshThumbnails;
            if (_thumbnailViewer.ListAllDocFileName == null || _thumbnailViewer.ListAllDocFileName.Count < 1)
            {
                SplashScreenManager.Stop(this);
            }

        }

        /// <summary>
        /// Lists the vignette_ import document progressed.
        /// </summary>
        /// <param name="p_oImageRioCollection">The p_o image rio collection.</param>
        private void ThumbnailViewerImportDocumentProgressed(System.Collections.ObjectModel.ObservableCollection<ImageRio> p_oImageRioCollection)
        {
            _listViewer.Images = p_oImageRioCollection;
        }

        /// <summary>
        /// Handles the RefreshThumbnailsForScan event of the m_oListVignetteFromImportPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ThumbnailViewerRefreshThumbnailsForScan(object sender, EventArgs e)
        {
            _thumbnailViewer.RefreshThumbnails -= ThumbnailViewerRefreshThumbnailsForScan;
        }

        #endregion

        #region Viewer

        /// <summary>
        /// Event raised when import is finalized 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewerControl_EndOfImport(object sender, EventArgs e)
        {
            if (DisplayMode == FolderPage.SelectModeDocument.Viewer)
            {
                _singleViewer.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId,
                                                    _thumbnailViewer.ListAllDocFileName, 0, false);
            }

            headMenu.StartNotification();
        }

        /// <summary>
        /// Event raised when selecting an item is selected in the thumbnail panel
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DocumentSelectedEventArgs"/> instance containing the event data.</param>
        private void SingleViewerItemsSelectedFileName(object sender, DocumentSelectedEventArgs args)
        {
            if (DisplayMode == FolderPage.SelectModeDocument.Viewer)
            {
                m_oControlImportPanel.m_oListSelectedFilename = args.ListAllSelectedFiles;
            }
        }

        /// <summary>
        /// Event raised when selecting an item in the viewer
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SingleViewerDisplayCompleted(object sender, EventArgs e)
        {
            SplashScreenManager.Stop(this);
        }

        /// <summary>
        /// Event raised when double click a directory in the viewer
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs"/> instance containing the event data.</param>
        private void SingleViewerDirectorySelectedToDisplay(object sender, DirectorySelectedEventArgs args)
        {
            _singleViewer.SelectedDocumentIndex = 0;
            _singleViewer.CurrentSelectedDirectory = args.DirectoryName;
            _singleViewer.CurrentSelectedImage = args.SelectedDirectory;

            _thumbnailViewer.SelectedDocumentIndex = 0;
            _thumbnailViewer.CurrentSelectedDirectory = args.DirectoryName;
            _thumbnailViewer.CurrentSelectedImage = args.SelectedDirectory;
            _thumbnailViewer.LoadVignettes(0);

            //**disable button import
            _singleViewer.ScanImportIsEnabled = args.DirectoryMode != DirectorySelectedEventArgs.Mode.OpenDirectory;

            _singleViewer.RefreshItems();
            _listViewer.Visibility = Visibility.Collapsed;
            _singleViewer.Visibility = Visibility.Visible;
            _thumbnailViewer.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Viewers the control_ imported document deleted.
        /// </summary>
        /// <param name="p_oImageRioDeleted">The p_o image rio deleted.</param>
        private void SingleViewerImportedDocumentDeleted(ImageRio p_oImageRioDeleted)
        {
            //In this case the deleted image must be removed from the listviewer
            if (_listViewer != null && _listViewer.Images != null && p_oImageRioDeleted != null)
            {
                if (_listViewer.Images.Contains(p_oImageRioDeleted))
                {
                    _listViewer.Images.Remove(p_oImageRioDeleted);
                }
            }
        }

        #endregion

        #region Index panel

        /// <summary>
        /// Handles the GoToThumbnailMode event of the m_oControlImportPanel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void m_oControlImportPanel_GoToThumbnailMode(object sender, EventArgs e)
        {
            DisplayMode = FolderPage.SelectModeDocument.Thumbnail;
        }

        /// <summary>
        /// Handles the BeforeIndexing event of the m_oControlImportPanel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void m_oControlImportPanel_BeforeIndexing(object sender, EventArgs e)
        {
            _listViewer.SetActuallyRankingList();
            _thumbnailViewer.SetActuallyRankingList();
        }

        #endregion

        #endregion

        #region Private methods

        /// <summary>
        /// Controls the base key up.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void ControlBaseKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && m_oControlImportPanel != null)
                m_oControlImportPanel.IndexFiles();
        }

        /// <summary>
        /// Initialize all viewers (Single, Thumbnails, List)
        /// </summary>
        private void InitListThumbnailsAndViewerControl()
        {
            ViewerControlPanel.Children.Clear();

            #region Single
            _singleViewer = new UCViewer(null, AppDisplayMode.Import)
            {
                Name = "_viewerControl",
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                Visibility = Visibility.Collapsed,
            };
            _singleViewer.SetValue(Grid.RowProperty, 0);
            _singleViewer.SetValue(Grid.ColumnProperty, 0);

            ViewerControlPanel.Children.Add(_singleViewer);
            DisplayMode = FolderPage.SelectModeDocument.Viewer;
            _singleViewer.DisplayCompleted += SingleViewerDisplayCompleted;

            #endregion

            #region Thumbnails
            _thumbnailViewer = new ThumbnailPanel
            {
                Name = "ListVignette",
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                Visibility = Visibility.Collapsed,
            };

            _thumbnailViewer.SetValue(Grid.RowProperty, 0);
            _thumbnailViewer.SetValue(Grid.ColumnProperty, 0);
            ViewerControlPanel.Children.Add(_thumbnailViewer);
            #endregion

            #region List
            _listViewer = new ListViewer(AppDisplayMode.Import)
            {
                Name = "_listViewer",
                Visibility = Visibility.Collapsed,
            };
            _listViewer.SetValue(Grid.RowProperty, 0);
            _listViewer.SetValue(Grid.ColumnProperty, 0);
            ViewerControlPanel.Children.Add(_listViewer);
            #endregion

        }

        /// <summary>
        /// Translation
        /// </summary>
        public void InitLabel()
        {
            _singleViewer.InitLabel();
            _thumbnailViewer.InitLabel();
            m_oControlImportPanel.InitLabel();
        }

        /// <summary>
        /// Actives the vignette mode.
        /// </summary>
        /// <param name="filesToImport">The files to import.</param>
        private void ActiveVignetteModeForImportation(IEnumerable<FileInfo> filesToImport = null)
        {
            _singleViewer.Visibility = Visibility.Collapsed;
            _listViewer.Visibility = Visibility.Collapsed;
            _singleViewer.ClearViewer();

            _thumbnailViewer.Visibility = Visibility.Visible;

            if (filesToImport != null)
            {
                _thumbnailViewer.LoadVignetteAndImport(filesToImport);
            }
            else
            {
                _thumbnailViewer.LoadVignettes();
            }

            m_oControlImportPanel.m_oListSelectedFilename = _thumbnailViewer.GetSelectedImage<ImageRio>();
        }

        /// <summary>
        /// Handles the CreateRefreshFileCompleted event of the L_oWCFServiceClient control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rio.Framework.WCFService.RioWcfServices.CreateRefreshFileCompletedEventArgs" /> instance containing the event data.</param>
        private void L_oWCFServiceClient_CreateRefreshFileCompleted(object sender, CreateRefreshFileCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Error On CreateRefreshFile() -> " + e.Error.Message, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessageGetDocument.ShowDialog();
                //CursorNormal(this);
            }
            else
            {
                ReturnBoolean l_oReturnBoolean = e.Result;
                if (l_oReturnBoolean.IsValid == false)
                {
                    MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Response not valid On CreateRefreshFile() -> " + l_oReturnBoolean.ErrorMessage, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessageGetDocument.ShowDialog();
                    //CursorNormal(this);
                }
                else
                {
                    if (Convert.ToBoolean(l_oReturnBoolean.Value))
                    {
                        m_dtRioscanTimer.Start();
                    }
                }
            }
        }

        /// <summary>
        /// Handles the Tick event of the m_dtRioscanTimer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void m_dtRioscanTimer_Tick(object sender, EventArgs e)
        {
            m_oWCFServiceClient.RefreshFileExistsAsync(m_sRefreshScanGuid, ServiceManager.GetLicenseKey());
        }

        /// <summary>
        /// Handles the RefreshFileExistsCompleted event of the L_oWCFServiceClient control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rio.Framework.WCFService.RioWcfServices.RefreshFileExistsCompletedEventArgs"/> instance containing the event data.</param>
        private void L_oWCFServiceClient_RefreshFileExistsCompleted(object sender, RefreshFileExistsCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Error On RefreshFileExist() -> " + e.Error.Message, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessageGetDocument.ShowDialog();
                m_dtRioscanTimer.Stop();
                headMenu.StartNotification();
            }
            else
            {
                ReturnBoolean l_oReturnBoolean = e.Result;
                if (l_oReturnBoolean.IsValid == false)
                {
                    MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Response not valid On RefreshFileExist() -> " + l_oReturnBoolean.ErrorMessage, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessageGetDocument.ShowDialog();
                    m_dtRioscanTimer.Stop();
                    headMenu.StartNotification();
                }
                else
                {
                    if (!Convert.ToBoolean(l_oReturnBoolean.Value))
                    {
                        selectAllImageAfterScan = true;
                        _thumbnailViewer.LoadVignettes(0);
                        m_dtRioscanTimer.Stop();
                        headMenu.StartNotification();
                    }
                }
            }
        }

        #endregion
    }
}